package com.facebook.drawee.drawable;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;

import com.facebook.common.internal.Preconditions;
import com.facebook.common.internal.VisibleForTesting;

import java.util.Arrays;

public final class RoundedCornersDrawable extends ForwardingDrawable implements Rounded {
    static final class EnumHolder {
        static int[] a = new int[Type.values().length];

        static {
            try {
                EnumHolder.a[Type.CLIPPING.ordinal()] = 1;
            } catch (NoSuchFieldError v0) {

            }

            try {
                EnumHolder.a[Type.OVERLAY_COLOR.ordinal()] = 2;
            } catch (NoSuchFieldError v0) {
            }
        }
    }

    public enum Type {
        OVERLAY_COLOR,
        CLIPPING
    }


    @VisibleForTesting
    private Type mType;
    private final float[] mRadii;
    @VisibleForTesting
    private float[] mBorderRadii;
    @VisibleForTesting
    private Paint mPaint;
    private boolean mIsCircle;
    private float mBorderWidth;
    private int mBorderColor;
    private int mOverlayColor;
    private float mPadding;
    private final Path mPath;
    private final Path mBorderPath;
    private final RectF mTempRectangle;

    public RoundedCornersDrawable(Drawable drawable) {
        super(Preconditions.checkNotNull(drawable));
        this.mType = Type.OVERLAY_COLOR;
        this.mRadii = new float[8];
        this.mBorderRadii = new float[8];
        this.mPaint = new Paint(1);
        this.mIsCircle = false;
        this.mBorderWidth = 0f;
        this.mBorderColor = 0;
        this.mOverlayColor = 0;
        this.mPadding = 0f;
        this.mPath = new Path();
        this.mBorderPath = new Path();
        this.mTempRectangle = new RectF();
    }

    public final void setPadding(float arg1) {
        this.mPadding = arg1;
        this.updatePath();
        this.invalidateSelf();
    }

    public final void setOverlayColor(int arg1) {
        this.mOverlayColor = arg1;
        this.invalidateSelf();
    }

    public final void setBorder(int arg1, float arg2) {
        this.mBorderColor = arg1;
        this.mBorderWidth = arg2;
        this.updatePath();
        this.invalidateSelf();
    }

    public final void setCircle(boolean arg1) {
        this.mIsCircle = arg1;
        this.updatePath();
        this.invalidateSelf();
    }

    public final void setRadii(float[] radii) {
        int v3 = 8;
        if (radii == null) {
            Arrays.fill(this.mRadii, 0f);
        } else {
            Preconditions.checkArgument(radii.length == v3, "radii should have exactly 8 values");
            System.arraycopy(radii, 0, this.mRadii, 0, v3);
        }

        this.updatePath();
        this.invalidateSelf();
    }

    private void updatePath() {
        float v5 = 2f;
        this.mPath.reset();
        this.mBorderPath.reset();
        this.mTempRectangle.set(this.getBounds());
        this.mTempRectangle.inset(this.mPadding, this.mPadding);
        if (this.mIsCircle) {
            this.mPath.addCircle(this.mTempRectangle.centerX(), this.mTempRectangle.centerY(), Math.min(this.mTempRectangle.width(), this.mTempRectangle.height()) / v5, Path.Direction.CW);
        } else {
            this.mPath.addRoundRect(this.mTempRectangle, this.mRadii, Path.Direction.CW);
        }

        this.mTempRectangle.inset(-this.mPadding, -this.mPadding);
        this.mTempRectangle.inset(this.mBorderWidth / v5, this.mBorderWidth / v5);
        if (this.mIsCircle) {
            this.mBorderPath.addCircle(this.mTempRectangle.centerX(), this.mTempRectangle.centerY(), Math.min(this.mTempRectangle.width(), this.mTempRectangle.height()) / v5, Path.Direction.CW);
        } else {
            for (int i = 0; i < 8; ++i) {
                this.mBorderRadii[i] = this.mRadii[i] + this.mPadding - this.mBorderWidth / v5;
            }

            this.mBorderPath.addRoundRect(this.mTempRectangle, this.mBorderRadii, Path.Direction.CW);
        }

        this.mTempRectangle.inset(-this.mBorderWidth / v5, -this.mBorderWidth / v5);
    }

    public final void setRadius() {
        Arrays.fill(this.mRadii, 0f);
        this.updatePath();
        this.invalidateSelf();
    }

    public final void draw(Canvas canvas) {
        float v2 = 2f;
        Rect v6 = this.getBounds();
        switch (EnumHolder.a[this.mType.ordinal()]) {
            case 1: {
                int v0 = canvas.save();
                this.mPath.setFillType(Path.FillType.EVEN_ODD);
                canvas.clipPath(this.mPath);
                super.draw(canvas);
                canvas.restoreToCount(v0);
                break;
            }
            case 2: {
                super.draw(canvas);
                this.mPaint.setColor(this.mOverlayColor);
                this.mPaint.setStyle(Paint.Style.FILL);
                this.mPath.setFillType(Path.FillType.INVERSE_EVEN_ODD);
                canvas.drawPath(this.mPath, this.mPaint);
                if (!this.mIsCircle) {
                    break;
                }

                float v7 = ((((float) (v6.width() - v6.height()))) + this.mBorderWidth) / v2;
                float v8 = ((((float) (v6.height() - v6.width()))) + this.mBorderWidth) / v2;
                if (v7 > 0f) {
                    canvas.drawRect(((float) v6.left), ((float) v6.top), (((float) v6.left)) + v7, ((float) v6.bottom), this.mPaint);
                    canvas.drawRect((((float) v6.right)) - v7, ((float) v6.top), ((float) v6.right), ((float) v6.bottom), this.mPaint);
                }

                if (v8 <= 0f) {
                    break;
                }

                canvas.drawRect(((float) v6.left), ((float) v6.top), ((float) v6.right), (((float) v6.top)) + v8, this.mPaint);
                canvas.drawRect(((float) v6.left), (((float) v6.bottom)) - v8, ((float) v6.right), ((float) v6.bottom), this.mPaint);
                break;
            }
        }

        if (this.mBorderColor != 0) {
            this.mPaint.setStyle(Paint.Style.STROKE);
            this.mPaint.setColor(this.mBorderColor);
            this.mPaint.setStrokeWidth(this.mBorderWidth);
            this.mPath.setFillType(Path.FillType.EVEN_ODD);
            canvas.drawPath(this.mBorderPath, this.mPaint);
        }
    }

    protected final void onBoundsChange(Rect arg1) {
        super.onBoundsChange(arg1);
        this.updatePath();
    }
}

